<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
	String basePath = "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <base href="/">
    <!-- 下拉框 -->
    <link rel="stylesheet" href="static/ace/css/chosen.css"/>
    <link rel="stylesheet" href="static/ace/css/datepicker.css"/>
    <link href="//cdn.bootcss.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet">
    <!-- jsp文件头和头部 -->
    <%@ include file="../index/top.jsp" %>
</head>
<body class="no-skin">
<!-- /section:basics/navbar.layout -->
<div class="main-container" id="main-container">
    <!-- /section:basics/sidebar -->
    <div class="main-content">
        <div class="main-content-inner">
            <div class="page-content">
                <div class="row">
                    <div class="col-xs-12">
                        <form action="areagoodscount/${msg }.do" name="areagoodscountForm" id="areagoodscountForm"
                              method="post">
                            <c:if test="${areagood.id !=null}">
                                <input type="hidden" name="id" id="id" value="${areagood.id }"/>
                            </c:if>
                            <input type="hidden" id="state" name="state" value="${state}"/>
                            <input type="hidden" id="baseId" name="baseId" value="${areagood.baseId}"/>
                            <input type="hidden" id="baseAreaId" name="baseAreaId" value="${areagood.baseAreaId}"/>
                            <input type="hidden" name="areaCode" id="areaCode" value="${areagood.areaCode }"/>
                            <input type="hidden" name="baseCode" id="baseCode" value="${areagood.baseCode }"/>
                            <input type="hidden" name="version" id="version" value="${areagood.version }"/>
                            <%-- <input type="hidden" name="oldNum" id="oldNum" value="${areagood.num }" /> --%>
                            <input type="hidden" name="flag" id="flag" value=""/>
                            <input type="hidden" name="goodsSpecId" id="goodsSpecId" value=""/>
                            <div id="zhongxin" style="padding-top: 13px;">
                                <table id="table_report" class="table table-striped table-bordered table-hover">
                                    <tr>
                                        <td style="width:79px;text-align: right;padding-top: 13px;">基地名称:</td>
                                        <td><input type="text" name="baseName" id="baseName"
                                                   value="${areagood.baseName }" readonly="readonly" maxlength="32"
                                                   title="基地名称" style="width:98%;"/></td>
                                    </tr>
                                    <tr>
                                        <td style="width:79px;text-align: right;padding-top: 13px;">区域名称:</td>
                                        <td><input type="text" name="areaName" id="areaName"
                                                   value="${areagood.areaName }" readonly="readonly" maxlength="32"
                                                   title="区域名称" style="width:98%;"/></td>
                                    </tr>
                                    <tr>
                                        <td style="width:79px;text-align: right;padding-top: 13px;">批次编号:</td>
                                        <td><input type="text" name="batchNumber" id="batchNumber"
                                                   value="${areagood.batchNumber }" onchange="hasNum()" maxlength="32"
                                                   title="区域名称" style="width:98%;"/></td>
                                    </tr>
                                    <tr>
                                        <td style="width:79px;text-align: right;padding-top: 13px;">商品规格:</td>
                                        <td><input type="text" name="goodName" id="goodName" value=""
                                                   readonly="readonly" maxlength="32" placeholder="这里输入商品规格"
                                                   title="商品规格" style="width:98%;"/>
                                            <a id="clickId" class="btn btn-xs btn-info">选择商品规格...</a></td>
                                    </tr>
                                    <tr>
                                        <td style="width:79px;text-align: right;padding-top: 13px;">种植日期:</td>
                                        <td><input id="birthday" name="birthday" placeholder="请选择种植日期"
                                                   value="${areagood.birthday }"/></td>
                                    </tr>
                                    <tr>
                                        <td style="width:79px;text-align: right;padding-top: 13px;">总数量:</td>
                                        <td><input type="number" name="num" id="num" value="${areagood.num }"
                                                   onchange="checkNum()" placeholder="这里输入总数量" title="总数量"
                                                   style="width:98%;"/></td>
                                    </tr>
                                </table>
                                <table id="table_report1" class="table table-striped table-bordered table-hover">
                                    <tr>
                                        <td style="text-align: center;" colspan="10">
                                            <c:if test="${state =='01'}">
                                                <a class="btn btn-mini btn-primary" onclick="save('${state}');">保存</a>
                                            </c:if>
                                            <a class="btn btn-mini btn-danger" onclick="top.Dialog.close();">取消</a>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div id="zhongxin2" class="center" style="display:none"><br/><br/><br/><br/><img
                                    src="static/images/jiazai.gif"/><br/><h4 class="lighter block green"></h4></div>
                        </form>
                    </div>
                    <!-- /.col -->
                </div>
                <!-- /.row -->
            </div>
            <!-- /.page-content -->
        </div>
    </div>
    <!-- /.main-content -->
</div>
<!-- /.main-container -->
<!-- basic scripts -->
<!-- 页面底部js¨ -->
<%@ include file="../index/foot.jsp" %>
<!-- ace scripts -->
<script src="static/ace/js/ace/ace.js"></script>
<!-- inline scripts related to this page -->
<!-- 下拉框 -->
<script src="static/ace/js/chosen.jquery.js"></script>
<!--提示框-->
<script type="text/javascript" src="static/js/jquery.tips.js"></script>
<script type="text/javascript" src="static/laydate/laydate.js"></script>
<!-- 删除时确认窗口 -->
<script src="static/ace/js/bootbox.js"></script>
</body>
<script type="text/javascript">
    $(top.hangge());

    var hrefUrl = function () {
        $("#clickId").on("click", function () {
            var seId = $.trim($("#goodsSpecId").val());
            top.jzts();
            var diag = new top.Dialog();
            diag.Drag = true;
            diag.Title = "选择商品规格";
            diag.URL = '<%=basePath%>areagoodscount/goodsSpecList.do?seId=' + seId;
            diag.Width = 800;
            diag.Height = 450;

            // 点击取消
            diag.CancelEvent = function () { //关闭事件
                /* if(diag.innerFrame.contentWindow.document.getElementById('zhongxin').style.display == 'none'){
                    if('
                ${page.currentPage}' == '0'){
						 top.jzts();
						 setTimeout("self.location=self.location",100);
					 }else{
						 
					 }
				} */
                diag.close();
            };
            // 点击确认
            diag.OKEvent = function () {
                var child = diag.innerFrame.contentWindow.document;
                var table = $(child).find('#simple-table')
                var inputs = table.find('tr input[type="radio"]:checked')
                if (inputs.length == 0) {
                    return alert('请选择规格!')
                }

                var goodsSpecId = $(child).find("#ids").val();
                var goodName = $(child).find("#title").val();
                $("#goodsSpecId").val(goodsSpecId);
                $("#goodName").val(goodName);
                diag.close();
            }
            diag.show(goodsSpecId);
        });
    }

    //保存
    function save(state) {
        if ($("#goodsSpecId").val() == "") {
            $("#goodName").tips({
                side: 3,
                msg: '请选择商品规格',
                bg: '#AE81FF',
                time: 2
            });
            $("#goodsSpecId").focus();
            return false;
        } else {
            $("#goodsSpecId").val($.trim($("#goodsSpecId").val()));
        }
        if ($.trim($("#batchNumber").val()) == "") {
            $("#batchNumber").tips({
                side: 3,
                msg: '请输入编码',
                bg: '#AE81FF',
                time: 2
            });
            $("#batchNumber").focus();
            return false;
        } else {
            $("#batchNumber").val($.trim($("#batchNumber").val()));
        }
        if ($.trim($("#birthday").val()) == "") {
            $("#birthday").tips({
                side: 3,
                msg: '请选择种植日期',
                bg: '#AE81FF',
                time: 2
            });
            $("#birthday").focus();
            return false;
        } else {
            $("#birthday").val($.trim($("#birthday").val()));
        }

        var num = $.trim($("#num").val());
        /* var oldNum = $.trim($("#oldNum").val());
        if(oldNum==""){
            oldNum=0;
        } */
        if (num == "") {
            $("#num").tips({
                side: 3,
                msg: '总数量必须大于0',
                bg: '#AE81FF',
                time: 2
            });
            return false;
        }

        if (parseInt(num) <= 0) {
            $("#num").tips({
                side: 3,
                msg: '总数量必须大于0',
                bg: '#AE81FF',
                time: 2
            });
            return false;
        }
        if (num.length > 5) {
            $("#num").tips({
                side: 3,
                msg: '总数量必须小于99999',
                bg: '#AE81FF',
                time: 2
            });
            return false;
        }
        /* if(parseInt(num)<parseInt(oldNum)){
            $("#num").tips({
                side:3,
                msg:'总数量增加，不能减少',
                bg:'#AE81FF',
                time:2
            });
            $("#num").val(oldNum)
            $("#name").focus();
            return false;
        } */

        var batchNumber = $.trim($("#batchNumber").val());
        var regCode = /^[a-zA-Z0-9]+$/;
        if (batchNumber == "") {
            $("#batchNumber").tips({
                side: 3,
                msg: '请输入批次编号',
                bg: '#AE81FF',
                time: 2
            });
            $("#batchNumber").focus();
            return false;
        } else {
            if (!regCode.test($("#batchNumber").val())) {
                $("#batchNumber").tips({
                    side: 3,
                    msg: '区域编号长度最大为32位，只能是字母或数字组成',
                    bg: '#AE81FF',
                    time: 2
                });
                $("#batchNumber").focus();
                return false;
            } else {
                $.ajax({
                    type: "POST",
                    url: '<%=basePath%>areagoodscount/hasNum.do',
                    data: {batchNumber: batchNumber, tm: new Date().getTime()},
                    dataType: 'json',
                    cache: false,
                    success: function (data) {
                        if ("success" != data.result) {
                            $("#batchNumber").tips({
                                side: 3,
                                msg: '编号 ' + batchNumber + ' 已存在',
                                bg: '#AE81FF',
                                time: 3
                            });
                            $("#batchNumber").focus();
                        } else {
                            //新增时，校验是否存在同基地，同区域，同规则，同种植日期
                            if ("01" == state) {
                                var baseId = $.trim($("#baseId").val());
                                var baseAreaId = $.trim($("#baseAreaId").val());
                                var goodsSpecId = $.trim($("#goodsSpecId").val());
                                var birthday = $.trim($("#birthday").val());
                                $.ajax({
                                    type: "POST",
                                    url: '<%=basePath%>areagoodscount/hasOnlyGood.do',
                                    data: {
                                        baseId: baseId,
                                        baseAreaId: baseAreaId,
                                        goodsSpecId: goodsSpecId,
                                        birthday: birthday,
                                        tm: new Date().getTime()
                                    },
                                    dataType: 'json',
                                    cache: false,
                                    success: function (data) {
                                        if ("success" == data.result) {//不存在，新增
                                            $("#areagoodscountForm").submit();
                                            $("#zhongxin").hide();
                                            $("#zhongxin2").show();
                                        } else if ("error" == data.result) {//存在，提示合并更新
                                            bootbox.confirm("已存在同基地，同区域，同规格，同种植日期的商品，是否合并？", function (result) {
                                                if (result) {//合并表示
                                                    $("#flag").val('1');
                                                    $("#areagoodscountForm").submit();
                                                    $("#zhongxin").hide();
                                                    $("#zhongxin2").show();
                                                }
                                            })
                                        } else {
                                            alert(data.result);
                                        }

                                    }
                                });
                            } else {
                                $("#areagoodscountForm").submit();
                                $("#zhongxin").hide();
                                $("#zhongxin2").show();
                            }
                        }
                    }
                });
            }
        }

    }


    //数量只能增不能小
    function checkNum() {
        var num = $.trim($("#num").val());
        /* var oldNum = $.trim($("#oldNum").val());
        if(oldNum==""){
            oldNum=0;
        } */
        if (num == "") {
            $("#num").tips({
                side: 3,
                msg: '总数量必须大于0',
                bg: '#AE81FF',
                time: 2
            });
            return false;
        }

        if (parseInt(num) <= 0) {
            $("#num").tips({
                side: 3,
                msg: '总数量必须大于0',
                bg: '#AE81FF',
                time: 2
            });
            return false;
        }
        if (num.length > 11) {
            $("#num").tips({
                side: 3,
                msg: '总数量必须小于99999999999',
                bg: '#AE81FF',
                time: 2
            });
            return false;
        }
    }

    //判断编号是否存在
    function hasNum() {
        var batchNumber = $.trim($("#batchNumber").val());
        var regCode = /^[a-zA-Z0-9]+$/;
        if (batchNumber == "") {
            $("#batchNumber").tips({
                side: 3,
                msg: '请输入批次编号',
                bg: '#AE81FF',
                time: 2
            });
            $("#batchNumber").focus();
            return false;
        } else {
            if (!regCode.test($("#batchNumber").val())) {
                $("#batchNumber").tips({
                    side: 3,
                    msg: '区域编号长度最大为32位，只能是字母或数字组成',
                    bg: '#AE81FF',
                    time: 2
                });
                $("#batchNumber").focus();
                return false;
            } else {
                $.ajax({
                    type: "POST",
                    url: '<%=basePath%>areagoodscount/hasNum.do',
                    data: {batchNumber: batchNumber, tm: new Date().getTime()},
                    dataType: 'json',
                    cache: false,
                    success: function (data) {
                        if ("success" != data.result) {
                            $("#batchNumber").tips({
                                side: 3,
                                msg: '编号 ' + batchNumber + ' 已存在',
                                bg: '#AE81FF',
                                time: 3
                            });
                            $("#batchNumber").focus();
                        }
                    }
                });
            }
        }
    }

    var birthday = {
        elem: '#birthday',
        format: 'YYYY-MM-DD',
        // min : laydate.now(),
        max: '2099-12-30',
        istime: true,
        istoday: true,
        choose: function (datas) {
        }
    };

    laydate(birthday);
    $(function () {
        var state = $("#state").val();
        if (state == 00) {
            $("#table_report").find("input,button,textarea,select").attr("disabled", "disabled");
            /* $("#table_report1 tr:gt(0)").remove(); */
            /* $("#table_report1").hide(); */
        } else {
            hrefUrl();
        }
    });

</script>
</html>